<?php
/**
 * Created by PhpStorm.
 * User: Dell
 * Date: 2018-12-25
 * Time: 14:24
 */

namespace app\common\model;


use app\admin\validate\GoodsValidate;
use think\Model;

class OrderModel extends Model
{
    // 确定链接表名
    protected $name = 'order';

    /**
     * 查询订单
     * @param $where
     * @param $offset
     * @param $limit
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getOrderByWhere($where, $offset, $limit)
    {
        return $this->where($where)->limit($offset, $limit)->order('orde_id desc')->select();
    }

    /**
     * 根据搜索条件获取所有的订单数量
     * @param $where
     * @return int|string
     */
    public function getAllOrder($where)
    {
        return $this->where($where)->count();
    }

    public function addOrder($param){

        try{
            $result = $this->validate('OrderValidate')->save($param);
            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());

            }else{

                return msg(1, '', '下单成功');
            }
        }catch (\Exception $e){
            return msg(-2, '', $e->getMessage());
        }
    }

    /**
     * 编辑订单信息
     * @param $param
     * @return array
     */
    public function editGoods($param)
    {
        try{

            $result = $this->validate('OrderValidate')->save($param, ['orde_id' => $param['orde_id']]);

            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());
            }else{

                return msg(1, url('order/index'), '编辑订单成功');
            }
        }catch(\Exception $e){
            return msg(-2, '', $e->getMessage());
        }
    }
}